<template>
    <el-dialog v-model="show" :title="title" align-center width="20%" @close="handleClose">
        <el-form :model="form" :rules="rules" ref="formRef" label-width="auto">
            <el-form-item label="定制类型" prop="customizedName">
                <el-input v-model="form.customizedName" style="width: 240px" placeholder="请输入定制类型" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="handleClose()">取 消</el-button>
                <el-button type="primary" @click="handleSubmit(formRef)">确 认</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { RuleForm } from '@/api/sj/productManagement/finalDraft/types';
import { type FormInstance, FormRules } from 'element-plus';
import { addFinalDraftApi, editFinalDraftApi } from '@/api/sj/productManagement/finalDraft';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const props = defineProps(['visible', 'initList', 'type']);
const emits = defineEmits(['submit', 'close']);
const formRef = ref<FormInstance>();
const form = ref<RuleForm>({ customizedName: "", id: '' });
const title = ref('新增定制类型');
const show = ref(false);

watch(() => props.visible, async (newValue) => {
    show.value = newValue;
}, { deep: true });

watch(() => props.initList, async (newValue) => {
    form.value = newValue;
}, { deep: true });

watch(() => props.type, async (newValue) => {
    console.log(props.initList, '123');
    
    if (newValue == 1) {
        title.value = '新增定制类型'
    } else if (newValue == 2) {
        title.value = '修改定制类型'
    } else {
        title.value = '定制类型详情'
    }
}, { deep: true });

const handleSubmit = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
        if (valid) {
            if (form.value.id) {
                const { code } = await editFinalDraftApi({
                    id: form.value.id, customizedName: form.value.customizedName
                });
                if (code == 200) {
                    emits('submit', true);
                    proxy?.$modal.msgSuccess('修改成功');
                    handleClose();
                } else {
                    emits('submit', false);
                };
            } else {
                const { code } = await addFinalDraftApi({ ...form.value });
                if (code == 200) {
                    emits('submit', true);
                    proxy?.$modal.msgSuccess('新增成功');
                    handleClose();
                } else {
                    emits('submit', false);
                };
            }

        } else {
            console.log('error submit!')
        }
    })
};

const rules: FormRules = reactive({
    customizedName: [
        { required: true, message: '请输入定制类型', trigger: 'blur' },
    ],
});

const handleClose = () => {
    formRef.value?.resetFields();
    emits('close');
};
</script>

<style lang="scss" scoped>
.el-select {
    width: 100%;
}
</style>